package com.vancouver.knows.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.DiscountInfo;
import com.vancouver.knows.business.bean.EventBannerInfo;
import com.vancouver.knows.business.bean.HomePageInfo;
import com.vancouver.knows.business.cache.PersistentCacheManager;

/**
 * 查询城惠列表解析
 * <p/>
 * Created by cxlin
 */
public class FetchCityDiscountsHandler extends ACheckableJsonParser {

	public static final String KEY_CATEGORIES = "categories";
	public static final String KEY_HEAD_POSTS = "head_posts";
	public static final String KEY_POSTS = "posts";
	public static final String BUSINESS_HOME_DEALS = "business_home_deals";

	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {
		String posts = content.getString("posts");
		if (StringUtil.isEmptyString(posts)) {
			resultMap.put(KEY_POSTS, new ArrayList<DiscountInfo>());
		} else {
			List<DiscountInfo> discountInfoList = JSON.parseArray(posts, DiscountInfo.class);
			resultMap.put(KEY_POSTS, discountInfoList);
		}

		String categories = content.getString("business_categories");
		if (StringUtil.isEmptyString(categories)) {
			resultMap.put(KEY_CATEGORIES, new ArrayList<DiscountCategoryInfo>());
		} else {
			List<DiscountCategoryInfo> discountCategoryInfoList = JSON.parseArray(categories,
					DiscountCategoryInfo.class);
			Collections.sort(discountCategoryInfoList, new BUtils.DiscountCategoryInfosSorter());
			resultMap.put(KEY_CATEGORIES, discountCategoryInfoList);
		}

		String headPosts = content.getString("head_posts");
		if (StringUtil.isEmptyString(headPosts)) {
			resultMap.put(KEY_HEAD_POSTS, new ArrayList<EventBannerInfo>());
		} else {
			List<EventBannerInfo> discountBannerInfoList = JSON.parseArray(headPosts, EventBannerInfo.class);
			resultMap.put(KEY_HEAD_POSTS, discountBannerInfoList);
		}

		String businessHomeDeals = content.getString("business_home_deals");
		if (StringUtil.isEmptyString(businessHomeDeals)) {
			resultMap.put(BUSINESS_HOME_DEALS, new ArrayList<HomePageInfo>());
		} else {
			List<HomePageInfo> businessHomeDealsList = JSON.parseArray(businessHomeDeals, HomePageInfo.class);
			resultMap.put(BUSINESS_HOME_DEALS, businessHomeDealsList);
		}

		PersistentCacheManager.getInstance().putObject(PersistentCacheManager.BUSINESS_HOME_CACHE_KEY, resultMap);
	}

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}
}
